package cn.fl.carloan.controller;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.*;
import java.util.regex.Pattern;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.api.ISysRegionApiService;
import cmm.comm.facade.framework.dto.SysRegionDTO;
import cmm.user.facade.system.api.IPriorityApiService;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.api.ISysUsrApiService;
import cmm.user.facade.system.condition.SysUsrFunAuthRQueryCondition;
import cmm.user.facade.system.condition.SysUsrQueryCondition;
import cmm.user.facade.system.dto.SysUsrDTO;
import cmm.user.facade.system.dto.SysUsrFunAuthRDTO;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.fl.carloan.service.IOdrPhoneNoteWebService;
import cn.fl.carloan.transform.OdrCompInfoVOTransform;
import cn.fl.carloan.vo.ListPageVoNew;
import cn.fl.carloan.vo.OdrCompInfoVO;
import cn.fl.carloan.vo.OdrPhoneNoteWebVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import fl.carloan.facade.carloan.api.IOdrChannelInfoApiService;
import fl.carloan.facade.carloan.api.IOdrCompChannelApiService;
import fl.carloan.facade.carloan.api.IOdrCompInfoApiService;
import fl.carloan.facade.carloan.condition.*;
import fl.carloan.facade.carloan.constant.OdrPhoneNoteWebConstant;
import fl.carloan.facade.carloan.dto.*;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;

/**
 * @Description: 控制层
 */
@Controller
@RequestMapping("/carloan/odrphonenoteweb")
public class OdrPhoneNoteWebController extends BaseController implements InitializingBean {

    private static final Logger logger = LoggerFactory.getLogger(OdrPhoneNoteWebController.class);

    /**
     * OdrPhoneNoteWeb 服务层BEAN
     */
    @Autowired
    private IOdrPhoneNoteWebService odrPhoneNoteWebService;

    @Autowired
    private IOdrCompChannelApiService odrCompChannelApiService;

    @Autowired
    private IOdrChannelInfoApiService odrChannelInfoApiService;

    @Autowired
    private IOdrCompInfoApiService odrCompInfoApiService;

    @Autowired
    private ISysRegionApiService sysRegionApiService;

    @Autowired
    private IPriorityApiService priorityApiService;

    @Autowired
    private ISysUsrApiService sysUsrApiService;

    private List<SysUsrDTO> sysUsrDTOS;

    @Autowired
    private ISysOrgApiService sysOrgApiService;

    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;


    /**
     * 到列表页面
     */
    @RequestMapping(value = "/toListPage.do", method = {RequestMethod.GET})
    public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
        Long startTm = System.currentTimeMillis();
        Long currentUserId = CurrentThreadContext.getCurrentUserId();
        BaseLogUtils.info(logger, "", "当前用户的用户ID为 " + currentUserId);
        if (currentUserId == null) {
            try {
                new ObjectMapper().writeValue(response.getOutputStream(), new DataResultVO(0, "没有获取到当前用户信息！"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //SysUsrDTO usrDTO = sysUsrApiService.selectSysUsrById(currentUserId, "11000", "0000");
        SysUsrFunAuthRQueryCondition usrFunAuthRQueryCondition = new SysUsrFunAuthRQueryCondition();
        usrFunAuthRQueryCondition.setSysCd("11000");
        usrFunAuthRQueryCondition.setUsrId(currentUserId);
        List<SysUsrFunAuthRDTO> sysUsrFunAuthRDTOS1 = priorityApiService.searchSysUsrFunAuthRDTOList(usrFunAuthRQueryCondition);
        List<Long> roleIds = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(sysUsrFunAuthRDTOS1)) {
            for (SysUsrFunAuthRDTO sysUsrFunAuthRDTO : sysUsrFunAuthRDTOS1) {
                if (sysUsrFunAuthRDTO.getRoleId() != null)
                    roleIds.add(sysUsrFunAuthRDTO.getRoleId());
            }
        }
        /*List<SysRoleDTO> sysRoleDTOList = usrDTO.getSysRoleDTOList();*/
        BaseLogUtils.info(logger, "", "当前用户的角色ID为 " + JSONObject.toJSONString(roleIds));
        if (CollectionUtils.isNotEmpty(roleIds)) {
            if (roleIds.contains(OdrPhoneNoteWebConstant.PHONE_NOTE_CHARGE_ROLE_ID_TEST)) {//如果是主管
                BaseLogUtils.info(logger, "toListPage", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
                SysUsrFunAuthRQueryCondition sysUsrFunAuthRQueryCondition = new SysUsrFunAuthRQueryCondition();
                sysUsrFunAuthRQueryCondition.setSysCd("11000");
                List<Long> roleIds2 = new ArrayList<>();
                 /*roleIds.add(181210152002682L);
                    roleIds.add(181210152002682L);*/
                //roleIds2.add(OdrPhoneNoteWebConstant.PHONE_NOTE_CHARGE_ROLE_ID_TEST);//
                roleIds2.add(OdrPhoneNoteWebConstant.PHONE_NOTE_EMPOLY_ROLE_ID_TEST);
                sysUsrFunAuthRQueryCondition.setRoleIdList(roleIds2);
                List<SysUsrFunAuthRDTO> sysUsrFunAuthRDTOS = priorityApiService.searchSysUsrFunAuthRDTOList(sysUsrFunAuthRQueryCondition);
                if (CollectionUtils.isNotEmpty(sysUsrFunAuthRDTOS)) {
                    List<Long> usrIds = new ArrayList<>();
                    for (SysUsrFunAuthRDTO usrFunAuthRDTO : sysUsrFunAuthRDTOS) {
                        if (!usrIds.contains(usrFunAuthRDTO.getUsrId()) && usrFunAuthRDTO.getUsrId() != null) {
                            if (!usrIds.contains(usrFunAuthRDTO.getUsrId())) {
                                usrIds.add(usrFunAuthRDTO.getUsrId());
                            }
                        }
                    }
                    if (CollectionUtils.isNotEmpty(usrIds)) {
                        SysUsrQueryCondition sysUsrQueryCondition = new SysUsrQueryCondition();
                        sysUsrQueryCondition.setUsrIds(usrIds);
                        sysUsrQueryCondition.setSysCd("11000");
                        this.sysUsrDTOS = sysUsrApiService.searchSysUsrList(sysUsrQueryCondition);
                        //this.sysUsrDTOS = sysUsrApiService.selectSysUserInfoList(usrIds);
                    }
                    BaseLogUtils.info(logger, "", "查询电销人员名单参数为：" + JSONObject.toJSONString(usrIds));
                }
                ModelAndView view = new ModelAndView("carloan/odrPhoneNoteWeb/odrPhoneNoteWebList");
                view.addObject("user", this.sysUsrDTOS);
                Long endTm = System.currentTimeMillis();
                BaseLogUtils.info(logger, "toListPage", "加载列表页面.耗时：" + (endTm - startTm), CurrentThreadContext.getCurrentUserName());
                return view;
            }
        }
        //如果是专员
        BaseLogUtils.info(logger, "toListPage", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
        ModelAndView view = new ModelAndView("carloan/odrPhoneNoteWeb/odrPhoneNoteWebListForEmploy");
        Long endTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "toListPage", "加载列表页面.耗时：" + (endTm - startTm), CurrentThreadContext.getCurrentUserName());
        return view;
    }

    /**
     * 到新增修改页面
     */
    @RequestMapping(value = "/toAddPage.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
        Long startTm = System.currentTimeMillis();
        String id = request.getParameter("id");
        String cstId = request.getParameter("cstId");
        OdrCompInfoDTO odrCompInfoDTO = odrCompInfoApiService.selectById(Long.valueOf(id));
        OdrCompChannelQC odrCompChannelQC = new OdrCompChannelQC();
        odrCompChannelQC.setCompId(Long.valueOf(id));
        odrCompChannelQC.setIsDel(0);
        List<OdrCompChannelDTO> odrCompChannelDTOS = odrCompChannelApiService.selectList(odrCompChannelQC);
        if (CollectionUtils.isNotEmpty(odrCompChannelDTOS)) {
            List<Long> channelIds = new ArrayList<>();
            for (OdrCompChannelDTO odrCompChannelDTO : odrCompChannelDTOS) {
                if (odrCompChannelDTO.getChannelId() != null)
                    channelIds.add(odrCompChannelDTO.getChannelId());
            }
            if (CollectionUtils.isNotEmpty(channelIds)) {
                OdrChannelInfoQC odrChannelInfoQC = new OdrChannelInfoQC();
                odrChannelInfoQC.setIsDel(0);
                odrChannelInfoQC.setIds(channelIds);
                List<OdrChannelInfoDTO> odrChannelInfoDTOS = odrChannelInfoApiService.selectList(odrChannelInfoQC);
                if (CollectionUtils.isNotEmpty(odrChannelInfoDTOS)) {
                    for (OdrChannelInfoDTO odrChannelInfoDTO : odrChannelInfoDTOS) {
                        if (StringUtils.isNotEmpty(cstId)) {
                            if (odrChannelInfoDTO.getId().equals(Long.valueOf(cstId))) {
                                odrCompInfoDTO.setOdrChannelInfoDTO(odrChannelInfoDTO);
                            }
                        } else {
                            odrCompInfoDTO.setOdrChannelInfoDTO(odrChannelInfoDTO);
                        }
                    }

                }
            }
        }

        BaseLogUtils.info(logger, "toAddPage", "加载新增.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());

        ModelAndView view = new ModelAndView("carloan/odrPhoneNoteWeb/odrPhoneNoteWebAdd");
        Map<String, String> positionMap = sysDictionaryApiService.searchGroupToMap("11000", "0000", "ODR0033");
        view.addObject("comp", odrCompInfoDTO);
        view.addObject("position", positionMap);
        Long endTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "toAddPage", "加载新增.耗时：" + (endTm - startTm), CurrentThreadContext.getCurrentUserName());
        return view;
    }

    /**
     * 到已拨打页面
     */
    @RequestMapping(value = "/toCallPage.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView toCallPage(HttpServletRequest request, HttpServletResponse response) {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "toAddPage", "加载修改页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
        BaseLogUtils.info(logger, "", "进入已拨打页面！");
        String id = request.getParameter("id");
        ModelAndView view = new ModelAndView("carloan/odrPhoneNoteWeb/odrPhoneNoteWebListForEmployCalled");
        Long endTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "toAddPage", "加载修改.耗时：" + (endTm - startTm), CurrentThreadContext.getCurrentUserName());
        return view;
    }


    /**
     * 到已分配页面
     */
    @RequestMapping(value = "/toHasFpPage.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView toHasFpPage(HttpServletRequest request, HttpServletResponse response) {
        Long startTm = System.currentTimeMillis();
        Long currentUserId = CurrentThreadContext.getCurrentUserId();
        BaseLogUtils.info(logger, "", "当前用户的用户ID为 " + currentUserId);
        if (currentUserId == null) {
            try {
                new ObjectMapper().writeValue(response.getOutputStream(), new DataResultVO(0, "没有获取到当前用户信息！"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //SysUsrDTO usrDTO = sysUsrApiService.selectSysUsrById(currentUserId, "11000", "0000");
        SysUsrFunAuthRQueryCondition usrFunAuthRQueryCondition = new SysUsrFunAuthRQueryCondition();
        usrFunAuthRQueryCondition.setSysCd("11000");
        usrFunAuthRQueryCondition.setUsrId(currentUserId);
        List<SysUsrFunAuthRDTO> sysUsrFunAuthRDTOS1 = priorityApiService.searchSysUsrFunAuthRDTOList(usrFunAuthRQueryCondition);
        List<Long> roleIds = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(sysUsrFunAuthRDTOS1)) {
            for (SysUsrFunAuthRDTO sysUsrFunAuthRDTO : sysUsrFunAuthRDTOS1) {
                if (sysUsrFunAuthRDTO.getRoleId() != null)
                    if (!roleIds.contains(sysUsrFunAuthRDTO.getRoleId())) {
                        roleIds.add(sysUsrFunAuthRDTO.getRoleId());
                    }
            }
        }
        /*List<SysRoleDTO> sysRoleDTOList = usrDTO.getSysRoleDTOList();*/
        BaseLogUtils.info(logger, "", "当前用户的角色ID为 " + JSONObject.toJSONString(roleIds));
        if (CollectionUtils.isNotEmpty(roleIds)) {
            if (roleIds.contains(OdrPhoneNoteWebConstant.PHONE_NOTE_CHARGE_ROLE_ID_TEST)) {//如果是主管
                BaseLogUtils.info(logger, "toListPage", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
                SysUsrFunAuthRQueryCondition sysUsrFunAuthRQueryCondition = new SysUsrFunAuthRQueryCondition();
                sysUsrFunAuthRQueryCondition.setSysCd("11000");
                List<Long> roleIds2 = new ArrayList<>();
                roleIds2.add(OdrPhoneNoteWebConstant.PHONE_NOTE_EMPOLY_ROLE_ID_TEST);
                sysUsrFunAuthRQueryCondition.setRoleIdList(roleIds2);
                List<SysUsrFunAuthRDTO> sysUsrFunAuthRDTOS = priorityApiService.searchSysUsrFunAuthRDTOList(sysUsrFunAuthRQueryCondition);
                if (CollectionUtils.isNotEmpty(sysUsrFunAuthRDTOS)) {
                    List<Long> usrIds = new ArrayList<>();
                    for (SysUsrFunAuthRDTO usrFunAuthRDTO : sysUsrFunAuthRDTOS) {
                        if (!usrIds.contains(usrFunAuthRDTO.getUsrId()) && usrFunAuthRDTO.getUsrId() != null) {
                            if (!usrIds.contains(usrFunAuthRDTO.getUsrId())) {
                                usrIds.add(usrFunAuthRDTO.getUsrId());
                            }
                        }
                    }
                    if (CollectionUtils.isNotEmpty(usrIds)) {
                        SysUsrQueryCondition sysUsrQueryCondition = new SysUsrQueryCondition();
                        sysUsrQueryCondition.setUsrIds(usrIds);
                        sysUsrQueryCondition.setSysCd("11000");
                        this.sysUsrDTOS = sysUsrApiService.searchSysUsrList(sysUsrQueryCondition);
                        //this.sysUsrDTOS = sysUsrApiService.selectSysUserInfoList(usrIds);
                    }
                    BaseLogUtils.info(logger, "", "查询电销人员名单参数为：" + JSONObject.toJSONString(usrIds));
                }
                ModelAndView view = new ModelAndView("carloan/odrPhoneNoteWeb/odrPhoneNoteWebListForChargeFp");
                view.addObject("user", this.sysUsrDTOS);
                Long endTm = System.currentTimeMillis();
                BaseLogUtils.info(logger, "toListPage", "加载列表页面.耗时：" + (endTm - startTm), CurrentThreadContext.getCurrentUserName());
                return view;
            }
        }
        ModelAndView view = new ModelAndView("carloan/odrPhoneNoteWeb/odrPhoneNoteWebListForChargeFp");
        Long endTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "toAddPage", "加载修改.耗时：" + (endTm - startTm), CurrentThreadContext.getCurrentUserName());
        return view;
    }

    /**
     * 到详情页面
     */
    @RequestMapping(value = "/toDetailPage.do", method = {RequestMethod.GET})
    public ModelAndView toDetailPage(HttpServletRequest request, HttpServletResponse response) {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "toDetailPage", "加载例子详细页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
        Map<String, String> intentionMap = sysDictionaryApiService.searchGroupToMap("11000", "0000", "ODR0034");
        String id = request.getParameter("id");
        Map<String, String> positionMap = sysDictionaryApiService.searchGroupToMap("11000", "0000", "ODR0033");
        OdrCompInfoDTO odrCompInfoDTO = odrCompInfoApiService.selectById(Long.valueOf(id));
        OdrPhoneNoteWebQC odrPhoneNoteWebQC = new OdrPhoneNoteWebQC();
        odrPhoneNoteWebQC.setIsDel(0);
        odrPhoneNoteWebQC.setCompId(Long.valueOf(id));
        List<OdrPhoneNoteWebDTO> odrPhoneNoteWebDTOS = odrPhoneNoteWebService.selectList(odrPhoneNoteWebQC);
        if (CollectionUtils.isNotEmpty(odrPhoneNoteWebDTOS)) {
            OdrPhoneNoteWebDTO odrPhoneNoteWebDTO = odrPhoneNoteWebDTOS.get(0);
            odrCompInfoDTO.setOdrPhoneNoteWebDTO(odrPhoneNoteWebDTO);
            Long cstId = odrPhoneNoteWebDTOS.get(0).getCstId();
            OdrChannelInfoDTO odrChannelInfoDTO1 = odrChannelInfoApiService.selectById(cstId);
            odrCompInfoDTO.setOdrChannelInfoDTO(odrChannelInfoDTO1);
        }
        OdrCompChannelQC odrCompChannelQC = new OdrCompChannelQC();
        odrCompChannelQC.setCompId(Long.valueOf(id));
        odrCompChannelQC.setIsDel(0);
        List<OdrCompChannelDTO> odrCompChannelDTOS = odrCompChannelApiService.selectList(odrCompChannelQC);
        BaseLogUtils.info(logger, "", "详情：当前查询到的关联的表信息为： " + JSONObject.toJSONString(odrCompChannelDTOS));
        if (CollectionUtils.isNotEmpty(odrCompChannelDTOS)) {
            List<Long> channelIds = new ArrayList<>();
            for (OdrCompChannelDTO odrCompChannelDTO : odrCompChannelDTOS) {
                if (odrCompChannelDTO.getChannelId() != null)
                    channelIds.add(odrCompChannelDTO.getChannelId());
            }
            BaseLogUtils.info(logger, "", "详情：当前查询到的channelIds表信息为： " + JSONObject.toJSONString(channelIds));
            if (CollectionUtils.isNotEmpty(channelIds)) {
                OdrChannelInfoQC odrChannelInfoQC = new OdrChannelInfoQC();
                odrChannelInfoQC.setIsDel(0);
                odrChannelInfoQC.setIds(channelIds);
                List<OdrChannelInfoDTO> odrChannelInfoDTOS = odrChannelInfoApiService.selectList(odrChannelInfoQC);
                if (CollectionUtils.isNotEmpty(odrChannelInfoDTOS)) {
                    OdrChannelInfoDTO needRemove = null;
                    Long needRemoveId = null;
                    for (OdrChannelInfoDTO odrChannelInfoDTO : odrChannelInfoDTOS) {
                        if (CollectionUtils.isEmpty(odrPhoneNoteWebDTOS)) {
                            odrCompInfoDTO.setOdrChannelInfoDTO(odrChannelInfoDTO);
//                            needRemove = odrChannelInfoDTO;
                            needRemoveId = odrChannelInfoDTO.getId();

                        } else {
//                            needRemove = odrChannelInfoDTO1;
                            Long cstId = odrPhoneNoteWebDTOS.get(0).getCstId();
                            OdrChannelInfoDTO odrChannelInfoDTO1 = odrChannelInfoApiService.selectById(cstId);
                            needRemoveId = odrChannelInfoDTO1.getId();
                        }

                    }

                    if (CollectionUtils.isNotEmpty(odrChannelInfoDTOS)) {
                        for (OdrChannelInfoDTO odrChannelInfoDTO : odrChannelInfoDTOS) {
                            odrChannelInfoDTO.setPosition(positionMap.get(odrChannelInfoDTO.getPosition()));
                        }
                    }
                    List<OdrChannelInfoDTO> dtoList = new ArrayList<>();
                    for (OdrChannelInfoDTO odrChannelInfoDTO : odrChannelInfoDTOS) {
                        if (!odrChannelInfoDTO.getId().equals(needRemoveId)) {
                            dtoList.add(odrChannelInfoDTO);
                        }
                    }
                    odrCompInfoDTO.setOtherChannels(dtoList);
                    if (CollectionUtils.isNotEmpty(dtoList)) {
                        odrCompInfoDTO.setIsOtherman("1");
                    } else {
                        odrCompInfoDTO.setIsOtherman("0");
                    }
                }
            }
        }
        if (isCharge()) {
            ModelAndView view = new ModelAndView("carloan/odrPhoneNoteWeb/OdrPhoneNoteWebForChargeDetail");
            if (odrCompInfoDTO != null) {
                OdrPhoneNoteWebDTO odrPhoneNoteWebDTO = odrCompInfoDTO.getOdrPhoneNoteWebDTO();
                if (odrPhoneNoteWebDTO != null) {
                    if (StringUtils.isNotEmpty(odrPhoneNoteWebDTO.getIntention())) {
                        odrPhoneNoteWebDTO.setIntentionNm(intentionMap.get(odrPhoneNoteWebDTO.getIntention()));
                    }
                }
            }
            view.addObject("comp", odrCompInfoDTO);
            view.addObject("isAdmin", isCharge() ? "1" : "0");
            return view;
        }

        ModelAndView view = new ModelAndView("carloan/odrPhoneNoteWeb/odrPhoneNoteWebDetail");

        view.addObject("comp", odrCompInfoDTO);
        view.addObject("isAdmin", isCharge() ? "1" : "0");
        Long endTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "toDetailPage", " 加载例子详细页面.耗时：" + (endTm - startTm), CurrentThreadContext.getCurrentUserName());
        return view;
    }

    /**
     * 初始化列表(待分配和已分配) 会出现在未分配列表里的是status=4 的，分配之后变成3
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/initList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initList(HttpServletRequest request, HttpServletResponse response) {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "initList", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());


        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        String isFp = request.getParameter("isFp");//是否分配
        String isCall = request.getParameter("isCall");//是否拨打
        String rgnProvCd = request.getParameter("rgnProvCd");//省code
        String rgnCityCd = request.getParameter("rgnCityCd");//是市code
        String isConneced = request.getParameter("isConneced");//是否接通
        String isKp = request.getParameter("isKp");//是否kp
        String intention = request.getParameter("intention");//客户意向
        String usrId = request.getParameter("usrId");//电销人员ID
        String isThreedayFollowIn = request.getParameter("isThreedayFollowIn");//电销人员ID
        if (StringUtils.isNotEmpty(isCall)) {
            //拨打和未拨打都是已分配
            isFp = "1";
        }

        Pager page = new Pager();
        if (StringUtils.isNotEmpty(currentPage)) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }

        if (StringUtils.isNotEmpty(pageSize)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }
        Map<String, String> intentionMap = sysDictionaryApiService.searchGroupToMap("11000", "0000", "ODR0034");
        if (StringUtils.isNotEmpty(isFp)) {//查询
            //查询未分配，只需查出来状态为4的的()
            if ("0".equals(isFp) && isCharge()) {
                OdrCompInfoQC odrCompInfoQC = new OdrCompInfoQC();
                odrCompInfoQC.setIsDel(0);
                odrCompInfoQC.setPrnCd(rgnProvCd);
                odrCompInfoQC.setCyCd(rgnCityCd);
                odrCompInfoQC.setDataStatus("4");
                if (StringUtils.isNotEmpty(isKp)) {
                    odrCompInfoQC.setIsKp(isKp);
                }
                odrCompInfoQC.setIsThreedayFollowIn(isThreedayFollowIn);
                if (StringUtils.isNotEmpty(intention) && !intention.equals("100")) {
                    List<String> intentions = new ArrayList<>();
                    intentions.add(intention);
                    odrCompInfoQC.setIntentions(intentions);
                }
                if (StringUtils.isNotEmpty(isConneced) && isConneced.equals("2")) {
                    isCall = "0";
                    isConneced = null;
                }
                odrCompInfoQC.setIsCall(isCall);
                if (StringUtils.isNotEmpty(isConneced)) {
                    List<String> isConneceds = new ArrayList<>();
                    isConneceds.add(isConneced);
                    odrCompInfoQC.setIsConneceds(isConneceds);
                }

                if (StringUtils.isNotEmpty(usrId)) {
                    odrCompInfoQC.setCrtUsrId(Long.valueOf(usrId));
                }
                page.setCondition(odrCompInfoQC);

                page = odrCompInfoApiService.selectListForPhoneNoteWebPage(page);
                List<OdrCompInfoDTO> resultList = (List<OdrCompInfoDTO>) page.getResultList();
                initNoteList(resultList);
            }
            if ("1".equals(isFp)) {//查询已分配的已拨打
                OdrCompInfoQC odrCompInfoQC = new OdrCompInfoQC();
                odrCompInfoQC.setIsDel(0);
                odrCompInfoQC.setPrnCd(rgnProvCd);
                odrCompInfoQC.setCyCd(rgnCityCd);
                odrCompInfoQC.setIsThreedayFollowIn(isThreedayFollowIn);
                if (StringUtils.isNotEmpty(isKp)) {
                    odrCompInfoQC.setIsKp(isKp);
                }
                if (!isCharge()) {
                    //如果不是主管只查自己的
                    BaseLogUtils.info(logger, "", "=====当前用户不是主管，只查询自己的已拨打的数据！========");
                    odrCompInfoQC.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
                }
                if (StringUtils.isNotEmpty(usrId)) {
                    odrCompInfoQC.setCrtUsrId(Long.valueOf(usrId));
                }
                if (StringUtils.isNotEmpty(isConneced) && isConneced.equals("2")) {
                    isCall = "0";
                    isConneced = null;
                }
                if (StringUtils.isNotEmpty(isConneced)) {
                    List<String> isConneceds = new ArrayList<>();
                    isConneceds.add(isConneced);
                    odrCompInfoQC.setIsConneceds(isConneceds);
                }
                odrCompInfoQC.setDataStatus("3");//3为已分配
                odrCompInfoQC.setIsCall(isCall);//是否拨打
                if (StringUtils.isNotEmpty(intention) && !intention.equals("100")) {
                    List<String> intentions = new ArrayList<>();
                    intentions.add(intention);
                    odrCompInfoQC.setIntentions(intentions);
                }
                page.setCondition(odrCompInfoQC);
                BaseLogUtils.info(logger, "", "=====查询已拨打的参数为Page ========" + JSONObject.toJSONString(page));
                page = odrCompInfoApiService.selectListForPhoneNoteWebPage(page);//selectListForPhoneNoteWebPage
                BaseLogUtils.info(logger, "", "=====查询已拨打的结果为： ========" + JSONObject.toJSONString(page));
                List<OdrCompInfoDTO> resultList = (List<OdrCompInfoDTO>) page.getResultList();
                initNoteList(resultList);//获取地区和人的信息
                if (CollectionUtils.isNotEmpty(resultList)) {
                    List<Long> compIdList = new ArrayList<>();
                    for (OdrCompInfoDTO odrCompInfoDTO : resultList) {
                        compIdList.add(odrCompInfoDTO.getId());
                    }
                    if (CollectionUtils.isNotEmpty(compIdList)) {
                        OdrPhoneNoteWebQC odrPhoneNoteWebQC = new OdrPhoneNoteWebQC();
                        odrPhoneNoteWebQC.setIsDel(0);
                        odrPhoneNoteWebQC.setCompIds(compIdList);
                        List<OdrPhoneNoteWebDTO> odrPhoneNoteWebDTOS = odrPhoneNoteWebService.selectList(odrPhoneNoteWebQC);
                        if (CollectionUtils.isNotEmpty(odrPhoneNoteWebDTOS)) {
                            for (OdrCompInfoDTO odrCompInfoDTO : resultList) {
                                for (OdrPhoneNoteWebDTO odrPhoneNoteWebDTO : odrPhoneNoteWebDTOS) {
                                    if (odrCompInfoDTO.getId().equals(odrPhoneNoteWebDTO.getCompId())) {
                                        Integer isKpDTO = odrPhoneNoteWebDTO.getIsKp();
                                        Integer isConnecedDTO = odrPhoneNoteWebDTO.getIsConneced();
                                        String intentionDTO = odrPhoneNoteWebDTO.getIntention();
                                        Integer isThreedayFollowin = odrPhoneNoteWebDTO.getIsThreedayFollowin();
                                        if (isKpDTO != null) {
                                            if (0 == isKpDTO) {
                                                odrPhoneNoteWebDTO.setIsKpNm("否");
                                            } else if (1 == isKpDTO) {
                                                odrPhoneNoteWebDTO.setIsKpNm("是");
                                            }
                                        }
                                        if (isConnecedDTO != null) {
                                            if (0 == isConnecedDTO) {
                                                odrPhoneNoteWebDTO.setIsConnectedNm("未接通");
                                            } else if (1 == isConnecedDTO) {
                                                odrPhoneNoteWebDTO.setIsConnectedNm("已接通");
                                            }
                                        }
                                        odrPhoneNoteWebDTO.setIntentionNm(intentionMap.get(intentionDTO));
                                        if (isThreedayFollowin != null) {
                                            if (0 == isThreedayFollowin) {
                                                odrPhoneNoteWebDTO.setIsThreedayFollowinNm("否");
                                            } else if (1 == isThreedayFollowin) {
                                                odrPhoneNoteWebDTO.setIsThreedayFollowinNm("是");
                                            }
                                        }
                                        odrCompInfoDTO.setOdrPhoneNoteWebDTO(odrPhoneNoteWebDTO);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        List<OdrCompInfoDTO> list = (List<OdrCompInfoDTO>) page.getResultList();
        ListPageVoNew<OdrCompInfoDTO> listPage = new ListPageVoNew<>(list, page.getTotalCount(), this.sysUsrDTOS);
        BaseLogUtils.info(logger, "", "需要返回的值为：" + JSONObject.toJSONString(listPage));
        Long endTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "initList", "加载列表页面.耗时：" + (endTm - startTm), CurrentThreadContext.getCurrentUserName());
        return listPage;
    }

    private void initNoteList(List<OdrCompInfoDTO> resultList) {
        if (CollectionUtils.isNotEmpty(resultList)) {
            List<Long> compIds = new ArrayList<>();
            for (OdrCompInfoDTO odrCompInfoDTO : resultList) {
                if (StringUtils.isNotEmpty(odrCompInfoDTO.getPrnCd())) {
                    SysRegionDTO regionByRgnCd = sysRegionApiService.getRegionByRgnCd(odrCompInfoDTO.getPrnCd(), "11000");
                    if (regionByRgnCd != null) {
                        odrCompInfoDTO.setPrnCd(regionByRgnCd.getRgnNm());
                    }
                }
                if (StringUtils.isNotEmpty(odrCompInfoDTO.getCyCd())) {
                    SysRegionDTO regionByRgnCd = sysRegionApiService.getRegionByRgnCd(odrCompInfoDTO.getCyCd(), "11000");
                    if (regionByRgnCd != null) {
                        odrCompInfoDTO.setCyCd(regionByRgnCd.getRgnNm());
                    }
                }
                //取出所有的公司ID 找到所有的联系人ID 然后 放入对象中
                compIds.add(odrCompInfoDTO.getId());
                for (SysUsrDTO sysUsrDTO : sysUsrDTOS) {
                    if (sysUsrDTO.getId().equals(odrCompInfoDTO.getCrtUsrId())) {
                        odrCompInfoDTO.setCrtUsrNm(sysUsrDTO.getRefRlNm());
                    }
                }
            }
            if (CollectionUtils.isNotEmpty(compIds)) {
                OdrPhoneNoteWebQC odrPhoneNoteWebQC = new OdrPhoneNoteWebQC();
                odrPhoneNoteWebQC.setCompIds(compIds);
                List<OdrPhoneNoteWebDTO> odrPhoneNoteWebDTOS = odrPhoneNoteWebService.selectList(odrPhoneNoteWebQC);
                if (CollectionUtils.isNotEmpty(odrPhoneNoteWebDTOS)) {
                    List<Long> cstIds = new ArrayList<>();
                    Map<Long, Long> compChannelIdMap = new HashMap<>();
                    for (OdrPhoneNoteWebDTO odrPhoneNoteWebDTO : odrPhoneNoteWebDTOS) {
                        if (odrPhoneNoteWebDTO.getCstId() != null && !cstIds.contains(odrPhoneNoteWebDTO.getCstId())) {
                            cstIds.add(odrPhoneNoteWebDTO.getCstId());
                        }
                        if (odrPhoneNoteWebDTO.getCompId() != null && odrPhoneNoteWebDTO.getCstId() != null) {
                            compChannelIdMap.put(odrPhoneNoteWebDTO.getCompId(), odrPhoneNoteWebDTO.getCstId());
                        }
                    }
                    if (CollectionUtils.isNotEmpty(cstIds)) {
                        OdrChannelInfoQC odrChannelInfoQC = new OdrChannelInfoQC();
                        odrChannelInfoQC.setIds(cstIds);
                        List<OdrChannelInfoDTO> odrChannelInfoDTOS = odrChannelInfoApiService.selectList(odrChannelInfoQC);
                        if (CollectionUtils.isNotEmpty(odrChannelInfoDTOS)) {
                            for (OdrCompInfoDTO odrCompInfoDTO : resultList) {
                                for (OdrChannelInfoDTO odrChannelInfoDTO : odrChannelInfoDTOS) {
                                    if (odrChannelInfoDTO.getId().equals(compChannelIdMap.get(odrCompInfoDTO.getId()))) {
                                        odrCompInfoDTO.setOdrChannelInfoDTO(odrChannelInfoDTO);
                                    }
                                }
                            }
                        }
                    }
                }

                /*OdrCompChannelQC odrCompChannelQC = new OdrCompChannelQC();
                odrCompChannelQC.setIsDel(0);
                odrCompChannelQC.setCompIds(compIds);
                //查询出所有的
                List<OdrCompChannelDTO> odrCompChannelDTOS = odrCompChannelApiService.selectList(odrCompChannelQC);
                if (CollectionUtils.isNotEmpty(odrCompChannelDTOS)) {
                    Map<Long, Long> compchannelMap = new HashMap<>();
                    List<Long> channelIds = new ArrayList<>();
                    for (OdrCompChannelDTO odrCompChannelDTO : odrCompChannelDTOS) {
                        channelIds.add(odrCompChannelDTO.getChannelId());
                        compchannelMap.put(odrCompChannelDTO.getCompId(), odrCompChannelDTO.getChannelId());
                    }
                    OdrChannelInfoQC odrChannelInfoQC = new OdrChannelInfoQC();
                    odrChannelInfoQC.setIds(channelIds);
                    odrChannelInfoQC.setIsDel(0);
                    List<OdrChannelInfoDTO> odrChannelInfoDTOS = odrChannelInfoApiService.selectList(odrChannelInfoQC);
                    if (CollectionUtils.isNotEmpty(odrChannelInfoDTOS)) {
                        for (OdrChannelInfoDTO odrChannelInfoDTO : odrChannelInfoDTOS) {
                            for (OdrCompInfoDTO odrCompInfoDTO : resultList) {
                                if (odrChannelInfoDTO.getId().equals(compchannelMap.get(odrCompInfoDTO.getId()))) {
                                    odrCompInfoDTO.setOdrChannelInfoDTO(odrChannelInfoDTO);
                                }
                            }
                        }
                    }
                }
            */
            }

            List<Long> compIdList = new ArrayList<>();
            List<OdrCompInfoDTO> compDtos = new ArrayList<>();
            for (OdrCompInfoDTO odrCompInfoDTO : resultList) {
                if (odrCompInfoDTO.getOdrChannelInfoDTO() == null) {
                    if (!compIdList.contains(odrCompInfoDTO.getId())) {
                        compIdList.add(odrCompInfoDTO.getId());
                        compDtos.add(odrCompInfoDTO);
                    }
                }
            }
            if (CollectionUtils.isNotEmpty(compIdList)) {
                OdrCompChannelQC odrCompChannelQC = new OdrCompChannelQC();
                odrCompChannelQC.setIsDel(0);
                odrCompChannelQC.setCompIds(compIdList);
                //查询出所有的
                List<OdrCompChannelDTO> odrCompChannelDTOS = odrCompChannelApiService.selectList(odrCompChannelQC);
                if (CollectionUtils.isNotEmpty(odrCompChannelDTOS)) {
                    Map<Long, Long> compchannelMap = new HashMap<>();
                    List<Long> channelIds = new ArrayList<>();
                    for (OdrCompChannelDTO odrCompChannelDTO : odrCompChannelDTOS) {
                        channelIds.add(odrCompChannelDTO.getChannelId());
                        compchannelMap.put(odrCompChannelDTO.getCompId(), odrCompChannelDTO.getChannelId());
                    }
                    OdrChannelInfoQC odrChannelInfoQC = new OdrChannelInfoQC();
                    odrChannelInfoQC.setIds(channelIds);
                    odrChannelInfoQC.setIsDel(0);
                    List<OdrChannelInfoDTO> odrChannelInfoDTOS = odrChannelInfoApiService.selectList(odrChannelInfoQC);
                    if (CollectionUtils.isNotEmpty(odrChannelInfoDTOS)) {
                        for (OdrChannelInfoDTO odrChannelInfoDTO : odrChannelInfoDTOS) {
                            if (CollectionUtils.isNotEmpty(compDtos)) {
                                for (OdrCompInfoDTO odrCompInfoDTO : compDtos) {
                                    if (odrChannelInfoDTO.getId().equals(compchannelMap.get(odrCompInfoDTO.getId()))) {
                                        odrCompInfoDTO.setOdrChannelInfoDTO(odrChannelInfoDTO);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }


    /**
     * 新增
     */
    @RequestMapping(value = "/add.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "add", "新增对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());

        String compId = request.getParameter("compId");
        String cstId = request.getParameter("cstId");
        String compNm = request.getParameter("compNm");
        String isConnected = request.getParameter("isConnected");
        String notConnecReason = request.getParameter("notConnectedReason");//未接通原因 1空号停机 2无人接听 3通话中/忙音
        String isReal = request.getParameter("isReal");
        String isKp = request.getParameter("isKp");
        String isThreedayFollowIn = request.getParameter("isThreedayFollowIn");
        String intention = request.getParameter("intention");
        String realAddress = request.getParameter("realAddress"); //(已接通，真实有效，可上门)
        String kpNm = request.getParameter("kpNm");
        String kpMp = request.getParameter("kpMp");
        String kpPosition = request.getParameter("kpPosition");
        String bz = request.getParameter("bz");

        String channelNm = request.getParameter("channelNm");
        String channelMp = request.getParameter("channelMp");
        String position = request.getParameter("position");

        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "新增成功!");
        OdrPhoneNoteWebVO entity = null;
        try {
            if (StringUtils.isEmpty(compId)) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("公司主键不能为空!");
                return result;
            }
            if (StringUtils.isEmpty(channelNm) || StringUtils.isEmpty(channelMp) || StringUtils.isEmpty(position)) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("联系人信息不能为空!");
                return result;
            }
            //String REGEX_MOBILE = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0-9]))\\d{8}$";
            // String REGEX_PHONE = "^((0\\d{2,3})-)(\\d{7,8})(-(\\d{3,}))?$";
            String REGEX_MOBILE = "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$";
            String REGEX_PHONE = "^((0\\d{2,3})-)(\\d{7,8})(-(\\d{3,}))?$";
            if (StringUtils.isNotEmpty(channelMp)) {
                if (!Pattern.matches(REGEX_MOBILE, channelMp) && !Pattern.matches(REGEX_PHONE, channelMp)) {
                    result.setSuccess(BaseConstant.IS_YESNO_NO);
                    result.setInfo("联系人电话1 格式错误!");
                    return result;
                }
            }

            if (StringUtils.isNotEmpty(kpMp)) {
                if (!Pattern.matches(REGEX_MOBILE, kpMp) && !Pattern.matches(REGEX_PHONE, kpMp)) {
                    result.setSuccess(BaseConstant.IS_YESNO_NO);
                    result.setInfo("联系人电话2 格式错误!");
                    return result;
                }
            }

            Long channelIdForChannel = null;
            // 添加之前先查一下是否已存在
            OdrPhoneNoteWebQC phoneNoteWebQC = new OdrPhoneNoteWebQC();
            phoneNoteWebQC.setCompId(Long.valueOf(compId));
            phoneNoteWebQC.setIsDel(0);
            List<OdrPhoneNoteWebDTO> odrPhoneNoteWebDTOS = odrPhoneNoteWebService.selectList(phoneNoteWebQC);
            if (CollectionUtils.isNotEmpty(odrPhoneNoteWebDTOS)) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("此公司已电话拜访过，不能重复拜访！");
                return result;
            }
            if (StringUtils.isEmpty(isConnected) || StringUtils.isEmpty(isThreedayFollowIn)) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("接通状况和三日内是否可拜访情况未填写!");
                return result;
            }

            if ("0".equals(isConnected)) {
                if (StringUtils.isEmpty(notConnecReason)) {
                    result.setSuccess(BaseConstant.IS_YESNO_NO);
                    result.setInfo("未接通原因不能为空!");
                    return result;
                }
                isReal = null;
                isKp = null;
                intention = null;
            }
            if ("1".equals(isConnected)) {
                if (StringUtils.isEmpty(isReal) || StringUtils.isEmpty(isKp) || StringUtils.isEmpty(intention)) {
                    result.setSuccess(BaseConstant.IS_YESNO_NO);
                    result.setInfo("是否真实有效、是否kp、客户意向不能为空!");
                    return result;
                }
            }
            /*if ("1".equals(isConnected) || StringUtils.isEmpty(isKp) || StringUtils.isEmpty(intention)) {
                if (StringUtils.isEmpty(isReal)) {
                    result.setSuccess(BaseConstant.IS_YESNO_NO);
                    result.setInfo("未接通原因、是否kp、客户意向不能为空!");
                    return result;
                }
            }*/
            if ("0".equals(isReal)) {
                realAddress = null;
            }
            Long currentUserId = CurrentThreadContext.getCurrentUserId();
            if (currentUserId == null) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("未获取到当前用户ID!");
                return result;
            }
            boolean flag = true;
            if (StringUtils.isEmpty(kpNm) && StringUtils.isEmpty(kpMp) && StringUtils.isEmpty(kpPosition)) {
                flag = false;
            }
            if (StringUtils.isNotEmpty(kpNm) && StringUtils.isNotEmpty(kpMp) && StringUtils.isNotEmpty(kpPosition)) {
                flag = false;
            }
            if (flag) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("新联系人参数不完整!");
                return result;
            }
            OdrChannelInfoDTO channelInfoDTO = new OdrChannelInfoDTO();
            if (StringUtils.isNotEmpty(cstId)) {
                channelInfoDTO.setId(Long.valueOf(cstId));
            }
            channelInfoDTO.setChannelMp(channelMp);
            channelInfoDTO.setChannelNm(channelNm);
            channelInfoDTO.setPosition(position);
            channelInfoDTO.setCrtTm(new Date());
            channelInfoDTO.setMdfTm(new Date());
            channelInfoDTO.setLnkCompId(Long.valueOf(compId));
            if ("0".equals(isReal) && StringUtils.isNotEmpty(cstId)) {
                channelInfoDTO.setIsDel(1);
                //删除与此公司的关联
                OdrCompChannelQC odrCompChannelQC = new OdrCompChannelQC();
                odrCompChannelQC.setCompId(Long.valueOf(compId));
                odrCompChannelQC.setChannelId(Long.valueOf(cstId));
                List<OdrCompChannelDTO> odrCompChannelDTOS = odrCompChannelApiService.selectList(odrCompChannelQC);
                List<Long> ids = new ArrayList<>();
                if (CollectionUtils.isNotEmpty(odrCompChannelDTOS)) {
                    for (OdrCompChannelDTO odrCompChannelDTO : odrCompChannelDTOS) {
                        ids.add(odrCompChannelDTO.getId());
                    }
                }
                odrCompChannelApiService.removeByIds(ids);
            }
            if (StringUtils.isNotEmpty(cstId)) {
                odrChannelInfoApiService.modifyOdrChannelInfoDTO(channelInfoDTO);
            } else {
                channelInfoDTO.setCompNm(compNm);
                channelInfoDTO.setCompTableNm("ODR_COMP_INFO");
                channelInfoDTO.setChannelTableNm("ODR_CHANNEL_INFO");
                if ("1".equals(isReal)) {
                    channelInfoDTO.setIsDel(0);
                } else if ("0".equals(isReal)) {
                    channelInfoDTO.setIsDel(1);
                }
                channelInfoDTO.setLnkCompId(Long.valueOf(compId));
                //=================================================

                //查一下手机号是不是已存在
                channelIdForChannel = addNewChannelGetId(channelMp, channelInfoDTO);
                //=================================================
                /*List<Long> chdChannelIds = odrChannelInfoService.getChdChanelIds();
                boolean flagForId = true;
                while (flagForId) {
                    long l = new Random().nextLong();
                    String s = String.valueOf(l);
                    StringBuilder append = new StringBuilder("20191150").append(s.substring(s.length() - 7, s.length()));
                    Long aLong = Long.valueOf(append.toString());
                    if (!chdChannelIds.contains(aLong)) {
                        channelInfoVO.setId(aLong);
                        flagForId = false;
                    }
                }*/

                cstId = String.valueOf(channelIdForChannel);
                //增加一个关联
                OdrCompChannelDTO odrCompChannelDTO = new OdrCompChannelDTO();
                odrCompChannelDTO.setIsDel(0);
                odrCompChannelDTO.setChannelId(channelIdForChannel);
                odrCompChannelDTO.setCompId(Long.valueOf(compId));
                odrCompChannelDTO.setCompTableNm("ODR_COMP_INFO");
                odrCompChannelDTO.setChannelTableNm("ODR_CHANNEL_INFO");
                odrCompChannelDTO.setCrtTm(new Date());
                odrCompChannelApiService.add(odrCompChannelDTO);

            }

            if (StringUtils.isNotEmpty(kpNm) && StringUtils.isNotEmpty(kpMp) && StringUtils.isNotEmpty(kpPosition)) {
                OdrChannelInfoDTO odrChannelInfoDTO = new OdrChannelInfoDTO();
                odrChannelInfoDTO.setPosition(kpPosition);
                odrChannelInfoDTO.setChannelNm(kpNm);
                odrChannelInfoDTO.setChannelMp(kpMp);
                odrChannelInfoDTO.setCompNm(compNm);
                odrChannelInfoDTO.setCompTableNm("ODR_COMP_INFO");
                odrChannelInfoDTO.setChannelTableNm("ODR_CHANNEL_INFO");
                odrChannelInfoDTO.setCrtUsrId(currentUserId);
                odrChannelInfoDTO.setIsDel(0);
                Long channelId = null;
                //查一下手机号是不是已存在
                channelId = addNewChannelGetId(kpMp, odrChannelInfoDTO);
                OdrCompChannelDTO odrCompChannelDTO = new OdrCompChannelDTO();
                odrCompChannelDTO.setCompId(Long.valueOf(compId));
                odrCompChannelDTO.setChannelId(channelId);
                odrCompChannelDTO.setCrtUsrId(currentUserId);
                odrCompChannelDTO.setCrtTm(new Date());
                odrCompChannelDTO.setCompTableNm("ODR_COMP_INFO");
                odrCompChannelDTO.setChannelTableNm("ODR_CHANNEL_INFO");
                odrCompChannelDTO.setIsDel(0);
                odrCompChannelApiService.add(odrCompChannelDTO);

            }

            entity = new OdrPhoneNoteWebVO();
            entity.setCompId(Long.valueOf(compId));
            entity.setCompNm(compNm);
            if (StringUtils.isNotEmpty(cstId)) {
                entity.setCstId(Long.valueOf(cstId));
            }
            if (StringUtils.isNotEmpty(intention)) {
                entity.setIntention(intention);
            }
            if (StringUtils.isNotEmpty(isReal)) {
                entity.setIsReal(Integer.valueOf(isReal));
            }
            if (StringUtils.isNotEmpty(isThreedayFollowIn)) {
                entity.setIsThreedayFollowin(Integer.valueOf(isThreedayFollowIn));
            }
            if (StringUtils.isNotEmpty(isConnected)) {
                entity.setIsConneced(Integer.valueOf(isConnected));
            }
            entity.setNotConnecedReason(notConnecReason);
            if (StringUtils.isNotEmpty(isKp)) {
                entity.setIsKp(Integer.valueOf(isKp));
            }
            if (StringUtils.isNotEmpty(realAddress)) {
                entity.setRealAddress(realAddress);
            }
            entity.setCommments(bz);
            entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
            Long id = this.odrPhoneNoteWebService.add(entity);

            OdrCompInfoDTO odrCompInfoDTO = new OdrCompInfoDTO();
            odrCompInfoDTO.setId(Long.valueOf(compId));
            odrCompInfoDTO.setDataStatus("3");//挂靠台数小于10台以下的
            odrCompInfoDTO.setCrtUsrId(currentUserId);
            if (StringUtils.isNotEmpty(realAddress)) {
                odrCompInfoDTO.setAddress(realAddress);
            }
            odrCompInfoDTO.setMdfTm(new Date());
            odrCompInfoApiService.modifyOdrCompInfoDTO(odrCompInfoDTO);

            result.setData(id);
            BaseLogUtils.info(logger, "add", "新增数据.[id=" + id + "] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
            BaseLogUtils.info(logger, "add", "新增对象失败,name[" + compNm + "].", CurrentThreadContext.getCurrentUserName(), ex);
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("系统错误,请联系管理员!");
            BaseLogUtils.error(logger, "add", "新增对象失败,name[" + compNm + "].", CurrentThreadContext.getCurrentUserName(), ex);
        }
        Long endTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "add", "新增对象.耗时：" + (endTm - startTm), CurrentThreadContext.getCurrentUserName());

        return result;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/modify.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "modify", "修改对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());

        String phoneNoteId = request.getParameter("phoneNoteId");
        String compNm = request.getParameter("compNm");
        String isConnected = request.getParameter("isConnected");
        String notConnecReason = request.getParameter("notConnectedReason");//未接通原因 1空号停机 2无人接听 3通话中/忙音
        String isReal = request.getParameter("isReal");
        String isKp = request.getParameter("isKp");
        String isCanVisitor = request.getParameter("isThreedayFollowIn");
        String intention = request.getParameter("intention");
        String realAddress = request.getParameter("realAddress");
        String bz = request.getParameter("bz");
        /*String kpNm = request.getParameter("kpNm");
        String kpMp = request.getParameter("kpMp");
        String kpPosition = request.getParameter("kpPosition");*/

        String channelNm = request.getParameter("channelNm");
        String channelMp = request.getParameter("channelMp");
        String channelposition = request.getParameter("position");

        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "更新成功!");
        OdrPhoneNoteWebVO entity = null;
        try {
            if (StringUtils.isEmpty(phoneNoteId)) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("电话拜访记录主键不能为空!");
                return result;
            }
            OdrPhoneNoteWebVO odrPhoneNoteVO = odrPhoneNoteWebService.selectById(Long.valueOf(phoneNoteId));
            if (odrPhoneNoteVO == null) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("当前电话记录不存在!");
                return result;
            }
            Long currentUserId = CurrentThreadContext.getCurrentUserId();
            if (currentUserId == null) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("未获取到当前用户ID!");
                return result;
            }
            OdrCompInfoDTO odrCompInfoDTO = odrCompInfoApiService.selectById(odrPhoneNoteVO.getCompId());
            if (!currentUserId.equals(odrCompInfoDTO.getCrtUsrId())) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("您无权修改此电话记录!");
                return result;
            }
            // String REGEX_MOBILE = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
            // String REGEX_PHONE = "^((0\\d{2,3})-)(\\d{7,8})(-(\\d{3,}))?$";
            String REGEX_MOBILE = "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$";
            String REGEX_PHONE = "^((0\\d{2,3})-)(\\d{7,8})(-(\\d{3,}))?$";
            if (StringUtils.isNotEmpty(channelMp)) {
                if (!Pattern.matches(REGEX_MOBILE, channelMp) && !Pattern.matches(REGEX_PHONE, channelMp)) {
                    result.setSuccess(BaseConstant.IS_YESNO_NO);
                    result.setInfo("联系人电话1 格式错误!");
                    return result;
                }
            }
            String compId = String.valueOf(odrPhoneNoteVO.getCompId());
            String cstId = String.valueOf(odrPhoneNoteVO.getCstId());

//            if (Integer.valueOf("1").equals(odrPhoneNoteVO.getIsConneced())) {
//////                result.setSuccess(BaseConstant.IS_YESNO_NO);
//////                result.setInfo("当前电话记录不允许修改!");
//////                return result;
//////            }


            if (StringUtils.isEmpty(compId) && StringUtils.isEmpty(cstId)) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("公司、联系人主键不能为空!");
                return result;
            }
            if (StringUtils.isEmpty(isConnected) || StringUtils.isEmpty(isCanVisitor)) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("接通状况和三日内是否可拜访情况未填写!");
                return result;
            }

            if ("0".equals(isConnected)) {
                if (StringUtils.isEmpty(notConnecReason)) {
                    result.setSuccess(BaseConstant.IS_YESNO_NO);
                    result.setInfo("未接通原因不能为空!");
                    return result;
                }
                isReal = null;
                isKp = null;
                intention = null;
            }
            if ("1".equals(isConnected)) {
                if (StringUtils.isEmpty(isReal) || StringUtils.isEmpty(isKp) || StringUtils.isEmpty(intention)) {
                    result.setSuccess(BaseConstant.IS_YESNO_NO);
                    result.setInfo("是否真实有效、是否kp、客户意向不能为空!");
                    return result;
                }
            }
            /*if ("0".equals(isReal)) {
                isKp = null;
                intention = null;
                isCanVisitor = null;
                realAddress = null;
            }*/
            /*if ("1".equals(isReal) && "1".equals(isConnected) && "1".equals(isCanVisitor)) {
                if (StringUtils.isEmpty(realAddress)) {
                    result.setSuccess(BaseConstant.IS_YESNO_NO);
                    result.setInfo("真实地址不能为空!");
                    return result;
                }
            }*/
            boolean flag = true;
            /*if (StringUtils.isEmpty(kpNm) && StringUtils.isEmpty(kpMp) && StringUtils.isEmpty(kpPosition)) {
                flag = false;
            }
            if (StringUtils.isNotEmpty(kpNm) && StringUtils.isNotEmpty(kpMp) && StringUtils.isNotEmpty(kpPosition)) {
                flag = false;
            }
            if (flag) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("kp参数不完整!");
                return result;
            }*/
            OdrChannelInfoDTO channelInfoVO = new OdrChannelInfoDTO();
            channelInfoVO.setId(Long.valueOf(cstId));
            channelInfoVO.setChannelMp(channelMp);
            channelInfoVO.setChannelNm(channelNm);
            channelInfoVO.setPosition(channelposition);
            channelInfoVO.setCrtTm(new Date());
            channelInfoVO.setMdfTm(new Date());
            if ("0".equals(isReal)) {
                channelInfoVO.setIsDel(1);
                //删除与此公司的关联
                OdrCompChannelQC odrCompChannelQC = new OdrCompChannelQC();
                odrCompChannelQC.setCompId(Long.valueOf(compId));
                odrCompChannelQC.setChannelId(Long.valueOf(cstId));
                List<OdrCompChannelDTO> odrCompChannelDTOS = odrCompChannelApiService.selectList(odrCompChannelQC);
                List<Long> ids = new ArrayList<>();
                if (CollectionUtils.isNotEmpty(odrCompChannelDTOS)) {
                    for (OdrCompChannelDTO odrCompChannelDTO : odrCompChannelDTOS) {
                        ids.add(odrCompChannelDTO.getId());
                    }
                }
                odrCompChannelApiService.removeByIds(ids);
            }

            odrChannelInfoApiService.modifyOdrChannelInfoDTO(channelInfoVO);

            /*if (StringUtils.isNotEmpty(kpNm) && StringUtils.isNotEmpty(kpMp) && StringUtils.isNotEmpty(kpPosition)) {
                OdrChannelInfoDTO odrChannelInfoVO = new OdrChannelInfoDTO();
                odrChannelInfoVO.setPosition(kpPosition);
                odrChannelInfoVO.setChannelNm(kpNm);
                odrChannelInfoVO.setChannelMp(kpMp);
                odrChannelInfoVO.setCompNm(compNm);
                odrChannelInfoVO.setCompTableNm("ODR_COMP_INFO");
                odrChannelInfoVO.setChannelTableNm("ODR_CHANNEL_INFO");
                odrChannelInfoVO.setIsDel(0);
                Long channelId = null;
                //查一下手机号是不是已存在
                channelId = addNewChannelGetId(kpMp, odrChannelInfoVO);
                OdrCompChannelDTO odrCompChannelVO = new OdrCompChannelDTO();
                odrCompChannelVO.setCompId(Long.valueOf(compId));
                odrCompChannelVO.setChannelId(channelId);
                odrCompChannelVO.setCrtUsrId(currentUserId);
                odrCompChannelVO.setCrtTm(new Date());
                odrCompChannelVO.setCompTableNm("ODR_COMP_INFO");
                odrCompChannelVO.setChannelTableNm("ODR_CHANNEL_INFO");
                odrCompChannelVO.setIsDel(0);
                odrCompChannelApiService.add(odrCompChannelVO);

            }*/

            entity = new OdrPhoneNoteWebVO();
            entity.setId(Long.valueOf(phoneNoteId));
            entity.setCompId(Long.valueOf(compId));
            entity.setCompNm(compNm);
            if (StringUtils.isNotEmpty(cstId)) {
                entity.setCstId(Long.valueOf(cstId));
            }
            if (StringUtils.isNotEmpty(intention)) {
                entity.setIntention(intention);
            }
            if (StringUtils.isNotEmpty(isReal)) {
                entity.setIsReal(Integer.valueOf(isReal));
            }
            if (StringUtils.isNotEmpty(isCanVisitor)) {
                entity.setIsThreedayFollowin(Integer.valueOf(isCanVisitor));
            }
            if (StringUtils.isNotEmpty(isConnected)) {
                entity.setIsConneced(Integer.valueOf(isConnected));
            }
            entity.setNotConnecedReason(notConnecReason);
            if (StringUtils.isNotEmpty(isKp)) {
                entity.setIsKp(Integer.valueOf(isKp));
            }
            if (StringUtils.isNotEmpty(realAddress)) {
                entity.setRealAddress(realAddress);
            }
            if (StringUtils.isNotEmpty(bz)) {
                entity.setCommments(bz);
            }
            entity.setMdfTm(new Date());
            this.odrPhoneNoteWebService.modify(entity);

            OdrCompInfoVO odrCompInfoVO = new OdrCompInfoVO();
            odrCompInfoVO.setId(Long.valueOf(compId));
            odrCompInfoVO.setDataStatus("3");
            odrCompInfoVO.setCrtUsrId(currentUserId);
            odrCompInfoVO.setCrtTm(new Date());
            if (StringUtils.isNotEmpty(realAddress)) {
                odrCompInfoVO.setAddress(realAddress);
            }
            odrCompInfoApiService.modifyOdrCompInfoDTO(OdrCompInfoVOTransform.toDTO(odrCompInfoVO));
            BaseLogUtils.info(logger, "add", "更新数据失败,name[" + compNm + "] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
            BaseLogUtils.info(logger, "add", "更新数据失败,name[" + compNm + "].", CurrentThreadContext.getCurrentUserName(), ex);
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("系统错误,请联系管理员!");
            BaseLogUtils.error(logger, "add", "更新数据失败,name[" + compNm + "].", CurrentThreadContext.getCurrentUserName(), ex);
        }

        Long endTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "modify", "修改对象.耗时：" + (endTm - startTm), CurrentThreadContext.getCurrentUserName());

        return result;
    }

    /**
     * 删除数据
     */
    @RequestMapping(value = "/removeById.do", method = {RequestMethod.POST})
    @ResponseBody
    public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "remove", "移除对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());

        String id = request.getParameter("id");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "删除成功!");
        if (StringUtils.isEmpty(id)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("主键不能为空!");
            return result;
        }
        try {
            this.odrPhoneNoteWebService.removeById(Long.valueOf(id));
            BaseLogUtils.info(logger, "removeById", "删除数据.[id=" + id + "]", CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
            BaseLogUtils.info(logger, "remove", "移除对象失败,主键[" + id + "].", CurrentThreadContext.getCurrentUserName(), ex);
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("系统错误,请联系管理员!");
            BaseLogUtils.error(logger, "remove", "移除对象失败,主键[" + id + "].", CurrentThreadContext.getCurrentUserName(), ex);
        }

        Long endTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "remove", "移除对象.耗时：" + (endTm - startTm), CurrentThreadContext.getCurrentUserName());

        return result;
    }

    /**
     * 得到一条记录
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getById.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "getById", "根据主键获取对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());

        String id = request.getParameter("id");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作成功!");
        if (StringUtils.isEmpty(id)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("主键不能为空!");
            BaseLogUtils.error(logger, "getById", "获取数据,主键不存在.[id=" + id + "]", CurrentThreadContext.getCurrentUserName());
            return result;
        }

        try {
            OdrPhoneNoteWebVO vo = this.odrPhoneNoteWebService.selectById(Long.valueOf(id));
            if (null == vo) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("数据不存在!");
                BaseLogUtils.info(logger, "getById", "获取数据,数据不存在.[id=" + id + "]", CurrentThreadContext.getCurrentUserName());
            }
            result.setData(vo);
            BaseLogUtils.info(logger, "getById", "获取数据.[id=" + id + "]", CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
            BaseLogUtils.info(logger, "getById", "根据主键获取对象失败,主键[" + id + "].", CurrentThreadContext.getCurrentUserName(), ex);
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("系统错误,请联系管理员!");
            BaseLogUtils.error(logger, "getById", "根据主键获取对象失败,主键[" + id + "].", CurrentThreadContext.getCurrentUserName(), ex);
        }

        Long endTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "getById", "根据主键获取对象.耗时：" + (endTm - startTm), CurrentThreadContext.getCurrentUserName());

        return result;
    }

    private Long addNewChannelGetId(String channelMp, OdrChannelInfoDTO channelInfoDTO) {
        Long channelIdForChannel;
        OdrChannelInfoQC odrChannelInfo = new OdrChannelInfoQC();
        odrChannelInfo.setIsDel(0);
        odrChannelInfo.setChannelMp(channelMp);
        List<OdrChannelInfoDTO> odrChannelInfoList = odrChannelInfoApiService.selectList(odrChannelInfo);
        Map<String, Long> channelIdMap = new HashMap<String, Long>();
        if (CollectionUtils.isNotEmpty(odrChannelInfoList)) {
            //说明此电话已存在
            for (OdrChannelInfoDTO odrChannelInfoDTO : odrChannelInfoList) {
                if (odrChannelInfoDTO.getId() != null) {
                    if (String.valueOf(odrChannelInfoDTO.getId()).startsWith("20191150")) {
                        if (odrChannelInfoDTO.getMdfTm() != null) {
                            Long channelInfoDTOId = odrChannelInfoDTO.getId();
                            channelIdMap.put("level1", channelInfoDTOId);
                        }
                        if (odrChannelInfoDTO.getMdfTm() == null) {
                            Long channelInfoDTOId = odrChannelInfoDTO.getId();
                            channelIdMap.put("level3", channelInfoDTOId);
                        }
                    } else if (!String.valueOf(odrChannelInfoDTO.getId()).startsWith("20191150")) {
                        Long channelInfoDTOId = odrChannelInfoDTO.getId();
                        channelIdMap.put("level2", channelInfoDTOId);
                    }
                }
            }

            if (channelIdMap.get("level1") == null) {
                if (channelIdMap.get("level2") == null) {
                    channelIdForChannel = channelIdMap.get("level3");
                } else {
                    channelIdForChannel = channelIdMap.get("level2");
                }
            } else {
                channelIdForChannel = channelIdMap.get("level1");
            }

        } else {
            BaseLogUtils.info(logger, "", "==========kpMp不存在！！！！！！！！");
            channelIdForChannel = odrChannelInfoApiService.add(channelInfoDTO);
        }
        return channelIdForChannel;
    }

    /**
     * 获取已经分配的列表（区分拨打未拨打）
     */

    @ResponseBody
    @RequestMapping(value = "/getCallList.do", method = RequestMethod.GET)
    public DataResultVO getCallList(HttpServletRequest request, HttpServletResponse response) {
        DataResultVO result = new DataResultVO(1, "获取列表成功！");
        String usrId = request.getParameter("usrId");//想要查看的人的usrId
        String isCall = request.getParameter("isCall");//是否已经拨打
        try {
            Long currentUserId = CurrentThreadContext.getCurrentUserId();//获取当前人的用户ID
            if (currentUserId == null) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("未获取到当前用户的ID");
                return result;
            }
            /*
            如果当前人是电销主管 则将传入的usrId 当作usrId 如果不传usrId 则查询所有人的usrId
            if(){

            }*/
            boolean isCharge = false;
            OdrCompInfoQC odrCompInfoQC = new OdrCompInfoQC();
            odrCompInfoQC.setDataStatus("3");
            if (isCharge) {
                if (StringUtils.isNotEmpty(usrId)) {
                    odrCompInfoQC.setCrtUsrId(Long.valueOf(usrId));
                }
            } else {
                odrCompInfoQC.setCrtUsrId(currentUserId);
            }
            odrCompInfoQC.setIsCall(isCall);
            Pager pager = new Pager();
            pager.setCondition(odrCompInfoQC);
            pager = odrCompInfoApiService.selectListForPhoneNoteWebPage(pager);
            result.setData(pager);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("查询失败，系统异常！");
            return result;
        }
        return result;
    }

    /**
     * 根据公司ID获取拜访记录的详情
     *
     * @return
     */
    public Object getPhoneNoteWebByCompId(HttpServletRequest request, HttpServletResponse response) {
        DataResultVO result = new DataResultVO(1, "根据公司ID获取拜访记录的详情 SUCCESS!");
        String compId = request.getParameter("compId");
        if (StringUtils.isEmpty(compId)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("主键不能为空!");
            BaseLogUtils.error(logger, "getPhoneNoteWebByCompId", "获取数据,主键不存在.[compId=" + compId + "]", CurrentThreadContext.getCurrentUserName());
            return result;
        }
        try {
            OdrPhoneNoteWebQC condition = new OdrPhoneNoteWebQC();
            condition.setCompId(Long.valueOf(compId));
            condition.setIsDel(0);
            List<OdrPhoneNoteWebDTO> odrPhoneNoteWebDTOS = odrPhoneNoteWebService.selectList(condition);
            result.setData(odrPhoneNoteWebDTOS);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("查询失败，系统异常！");
            return result;
        }
        return result;
    }

    public boolean isCharge() {
        Long currentUserId = CurrentThreadContext.getCurrentUserId();
        SysUsrFunAuthRQueryCondition usrFunAuthRQueryCondition = new SysUsrFunAuthRQueryCondition();
        usrFunAuthRQueryCondition.setSysCd("11000");
        usrFunAuthRQueryCondition.setUsrId(currentUserId);
        List<SysUsrFunAuthRDTO> sysUsrFunAuthRDTOS1 = priorityApiService.searchSysUsrFunAuthRDTOList(usrFunAuthRQueryCondition);
        List<Long> roleIds = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(sysUsrFunAuthRDTOS1)) {
            for (SysUsrFunAuthRDTO sysUsrFunAuthRDTO : sysUsrFunAuthRDTOS1) {
                if (sysUsrFunAuthRDTO.getRoleId() != null) {
                    if (!roleIds.contains(sysUsrFunAuthRDTO.getRoleId())) {
                        roleIds.add(sysUsrFunAuthRDTO.getRoleId());
                    }
                }
            }
        }
        if (CollectionUtils.isNotEmpty(roleIds)) {
            if (roleIds.contains(OdrPhoneNoteWebConstant.PHONE_NOTE_CHARGE_ROLE_ID_TEST)) {//如果是主管
                return true;
            }
        }
        return false;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        SysUsrFunAuthRQueryCondition sysUsrFunAuthRQueryCondition = new SysUsrFunAuthRQueryCondition();
        sysUsrFunAuthRQueryCondition.setSysCd("11000");
        List<Long> roleIds2 = new ArrayList<>();
        roleIds2.add(OdrPhoneNoteWebConstant.PHONE_NOTE_EMPOLY_ROLE_ID_TEST);
        sysUsrFunAuthRQueryCondition.setRoleIdList(roleIds2);
        List<SysUsrFunAuthRDTO> sysUsrFunAuthRDTOS = priorityApiService.searchSysUsrFunAuthRDTOList(sysUsrFunAuthRQueryCondition);
        if (CollectionUtils.isNotEmpty(sysUsrFunAuthRDTOS)) {
            List<Long> usrIds = new ArrayList<>();
            for (SysUsrFunAuthRDTO usrFunAuthRDTO : sysUsrFunAuthRDTOS) {
                if (!usrIds.contains(usrFunAuthRDTO.getUsrId()) && usrFunAuthRDTO.getUsrId() != null) {
                    if (!usrIds.contains(usrFunAuthRDTO.getUsrId())) {
                        usrIds.add(usrFunAuthRDTO.getUsrId());
                    }
                }
            }
            if (CollectionUtils.isNotEmpty(usrIds)) {
                SysUsrQueryCondition sysUsrQueryCondition = new SysUsrQueryCondition();
                sysUsrQueryCondition.setUsrIds(usrIds);
                sysUsrQueryCondition.setSysCd("11000");
                this.sysUsrDTOS = sysUsrApiService.searchSysUsrList(sysUsrQueryCondition);
                //this.sysUsrDTOS = sysUsrApiService.selectSysUserInfoList(usrIds);
            }
            BaseLogUtils.info(logger, "", "查询电销人员名单参数为：" + JSONObject.toJSONString(usrIds));
            //190730165617995,190613165593384,190410165568625,190410165568512,190410165568499,190410165568513,190410165568491,190410165568506,190410165568490,190410165568458,190410165568467,190410165568439,181225165533065
        }
    }
}

